package com.tbvtc.agriculturalcreditchainserver2025.utils;

import org.geotools.data.DataStoreFinder;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.FeatureIterator;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.opengis.feature.simple.SimpleFeature;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

@Component
public class PreciseChinaClimateService {

    private SimpleFeatureCollection climateFeatures;
    private final GeometryFactory geometryFactory = new GeometryFactory();

    @Value("${climate.shapefile.path}")
    private String shapefilePath; // 从配置文件注入路径

    @PostConstruct //  在类初始化时执行
    public void init() throws IOException {
        File file = new File(shapefilePath);
        Map<String, Object> map = new HashMap<>();
        map.put("url", file.toURI().toURL());

        ShapefileDataStore dataStore = (ShapefileDataStore) DataStoreFinder.getDataStore(map);
        dataStore.setCharset(Charset.forName("GBK"));

        try {
            String typeName = dataStore.getTypeNames()[0];
            SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
            this.climateFeatures = source.getFeatures();
        } finally {
            dataStore.dispose();
        }
    }

    public  String getClimateType(double latitude, double longitude) {
        Point queryPoint = geometryFactory.createPoint(new Coordinate(longitude, latitude));

        try (FeatureIterator<SimpleFeature> iterator = climateFeatures.features()) {
            while (iterator.hasNext()) {
                SimpleFeature feature = iterator.next();
                         // 先获取几何对象并缓存引用
                Object defaultGeometry = feature.getDefaultGeometry();
                if (defaultGeometry instanceof org.locationtech.jts.geom.Geometry) {
                    // 强制类型转换
                    org.locationtech.jts.geom.Geometry geometry = (org.locationtech.jts.geom.Geometry) defaultGeometry;

                    // 第二步验证几何关系
                    if (geometry.contains(queryPoint)) {
                        // 保持原有变量作用域
                        String climateTypeAttributeName = "CliTypeCHN";
                        String climateType = (String) feature.getAttribute(climateTypeAttributeName);
                        System.out.println(climateType);
                        // 返回结果保持原有逻辑
                        return climateType != null ? climateType : "未知气候类型";
                    }
                }

// 隐含的 else 返回 (根据原代码上下文可能需要补充默认返回)
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "未知气候类型";
    }



}